# -*- coding: utf-8 -*-
"""
超市销售数据分析
实验13 统计分析一般类产品的每周销售金额
@author: tange
"""

import pandas as pd

data = pd.read_csv(r'C:\大数据分析综合实训-附件.csv', encoding='gbk')

# 数据转换
data['销售日期'] = pd.to_datetime(data['销售日期'], format='%Y%m%d', errors='ignore')

data0 = data[['商品类型', '销售日期', '销售金额']]

data0 = data0[data0['商品类型'] == '一般商品']

weeks = [[20150101, 20150104],
         [20150105, 20150111],
         [20150112, 20150118],
         [20150119, 20150125],
         [20150126, 20150131]
         ]

results = []

for week in weeks:

    start_time = pd.to_datetime(week[0], format='%Y%m%d', errors='ignore')
    end_time   = pd.to_datetime(week[1], format='%Y%m%d', errors='ignore')

    week_data = data0[data['销售日期'] <= end_time][data['销售日期'] >= start_time]
#    week_data = week_data.groupby('销售日期').sum()

    week_sum = week_data.销售金额.sum()

    week_result = ['一般商品', week_sum, (end_time - start_time).days + 1]
#    week_result = ['一般商品', week_sum, len(week_data)]

    results.append(week_result)
